﻿using Devonline.Core;
using IdentityServer4;
using IdentityServer4.Configuration;
using Microsoft.AspNetCore.Authentication.OAuth;

namespace Devonline.Identity.Admin
{
    public class AdminSetting : AppSetting
    {
        /// <summary>
        /// Github 认证配置
        /// </summary>
        public AuthOptions GitHub { get; set; }
        /// <summary>
        /// 微信认证配置
        /// </summary>
        public AuthOptions Weixin { get; set; }
        /// <summary>
        /// 支付宝认证配置
        /// </summary>
        public AuthOptions Alipay { get; set; }
        /// <summary>
        /// QQ 认证配置
        /// </summary>
        public AuthOptions QQ { get; set; }

        /// <summary>
        /// Options for aspects of the user interface.
        /// </summary>
        public UserInteractionOptions UserInteraction { get; set; }

        /// <summary>
        /// DataProtection
        /// </summary>
        public DataProtectionOptions DataProtection { get; set; }
    }

    /// <summary>
    /// auth options
    /// </summary>
    public class AuthOptions
    {
        public AuthOptions()
        {
            SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
        }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enable { get; set; }
        /// <summary>
        /// 图标
        /// </summary>
        public string Icon { get; set; }
        //
        // 摘要:
        //     Gets or sets the provider-assigned client id.
        public string ClientId { get; set; }
        //
        // 摘要:
        //     Gets or sets the provider-assigned client secret.
        public string ClientSecret { get; set; }
        //
        // 摘要:
        //     Gets the list of permissions to request.
        public string[] Scope { get; set; }

# nullable enable
        //
        // 摘要:
        //     Gets or sets the authentication scheme corresponding to the middleware responsible
        //     of persisting user's identity after a successful authentication. This value typically
        //     corresponds to a cookie middleware registered in the Startup class. When omitted,
        //     Microsoft.AspNetCore.Authentication.AuthenticationOptions.DefaultSignInScheme
        //     is used as a fallback value.
        public string? SignInScheme { get; set; }
#nullable disable

        /// <summary>
        /// 自配置方法
        /// </summary>
        /// <param name="options"></param>
        public void Configure(OAuthOptions options)
        {
            options.ClientId = ClientId;
            options.ClientSecret = ClientSecret;
            options.SignInScheme ??= SignInScheme;
            if (Scope.IsNotNullOrEmpty())
            {
                options.Scope.AddRange(Scope);
            }
        }
    }
}